﻿using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Events;
using Autodesk.Revit.UI.Selection;
using FloatDemo.Methods;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using static System.Net.Mime.MediaTypeNames;
using System.Windows.Media;
using System.Windows.Forms;
using FloatDemo.Models;

namespace FloatDemo.Commands
{
    [Transaction(TransactionMode.Manual)]
    public class CreatePipesTagCmd : IExternalCommand
    {
        UIDocument _uidoc;
        List<Line> _pipeLineList = new List<Line>();
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            _uidoc = uiapp.ActiveUIDocument;
            Document doc = _uidoc.Document;
            MouseHook mh;
            mh = new MouseHook();
            mh.SetHook();
            try
            {
                List<Reference> selRefList = _uidoc.Selection.PickObjects(ObjectType.Element, new PipeSelection(), "选择平行的管道").ToList();
                if (selRefList.Count == 0) return Result.Succeeded;
                _pipeLineList = selRefList.Select(x => doc.GetElement(x)).OfType<Pipe>().Select(x => (x.Location as LocationCurve).Curve.SetZ(0) as Line).ToList();
                FloatWindows.Instance.ChangeLocation(GetRectangle(_uidoc));
                mh.MouseMove = MouseMoveChange;
                FloatWindows.Instance.Show();
                XYZ pickPoint = _uidoc.Selection.PickPoint(ObjectSnapTypes.Points, "选择标签放置点");
            }
            catch { }
            mh.UnHook();
            FloatWindows.Instance.Closed();
            System.Windows.MessageBox.Show("后面的自己补充吧");
            return Result.Succeeded;
        }

        public void MouseMoveChange(System.Windows.Point point)
        {
            Rectangle rectangle = GetRectangle(_uidoc);
            System.Windows.Point tempPoint = new System.Windows.Point(point.X - rectangle.Left, point.Y - rectangle.Top);
            if (tempPoint.X < 0 || tempPoint.Y < 0) return;
            FloatWindows.Instance.CanvasMousePoint = tempPoint;
            if (!FloatWindows.Instance.PointIsMove()) return;
            FloatWindows.Instance.ChangeLocation(rectangle);
            XYZ movePoint = CanvasPointMethod.GetRevitPoint(_uidoc, FloatWindows.Instance.CanvasMousePoint);
            CanvasPoint moveCanvasPoint = CanvasPointMethod.GetCanvasPoint(_uidoc, movePoint);
            double dd = 0;
            foreach (var pipeLine in _pipeLineList)
            {
                if (!pipeLine.CanProject(movePoint)) continue;
                XYZ proPoint = pipeLine.GetProjectPoint(movePoint);
                XYZ pointMoveDirection = (proPoint - movePoint).Normalize();
                CanvasPoint p2 = CanvasPointMethod.GetCanvasPoint(_uidoc, proPoint);
                CanvasPoint p3 = CanvasPointMethod.GetCanvasPoint(_uidoc, movePoint.Add(pointMoveDirection * dd));
                CanvasPoint p4 = CanvasPointMethod.GetCanvasPoint(_uidoc, movePoint.Add(pointMoveDirection * dd).Add(XYZ.BasisX * 10));
                FloatWindows.Instance.Add(p2, moveCanvasPoint);
                FloatWindows.Instance.Add(p3, p4);
                dd += (300/304.8);
            }
        }

        public Rectangle GetRectangle(UIDocument uidoc)
        {
            var uiviews = uidoc.GetOpenUIViews();
            return uiviews[0].GetWindowRectangle();
        }
    }
    public class PipeSelection : ISelectionFilter
    {
        public bool AllowElement(Element elem)
        {
            if (elem is Pipe) return true;
            return false;
        }

        public bool AllowReference(Reference reference, XYZ position)
        {
            return true;
        }
    }
}
